package in.co.codedoc.sql.mapping;

import in.co.codedoc.cg.annotations.DBType;
import in.co.codedoc.cg.annotations.IsADBInputColumnMap;
import in.co.codedoc.sql.DBInputColumnMap;
import in.co.codedoc.sql.SQLStatement;
import in.co.codedoc.time.Day;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;

@IsADBInputColumnMap(dbType=DBType.DATE,javaType=Day.class)
public class DayDateInputColumnMap
	extends DBInputColumnMap
{
	public DayDateInputColumnMap()
	{
		SetNull();
	}
	
	public DayDateInputColumnMap(Day day)
	{
		v = day;
		if(v == null)
		{
			SetNull();
		}
	}
	
	@Override
	public void SetParameter(int index, PreparedStatement ps)
		throws SQLException
	{
		if(Logger.logger.isDebugEnabled())
		{
			if(index > 1)
			{
				SQLStatement.GetDebugStringBuffer().append(",");
			}
		}

		if(IsNull())
		{
			if(Logger.logger.isDebugEnabled())
			{
				SQLStatement.GetDebugStringBuffer().append("<NULL>");
			}
			
			ps.setNull(index, Types.TIMESTAMP);
		}
		else
		{
			if(Logger.logger.isDebugEnabled())
			{
				SQLStatement.GetDebugStringBuffer()
					.append('"')
					.append(v)
					.append('"');
			}
			
			Date date = new Date(v.GetCalendar().getTimeInMillis());
			
			ps.setDate(index,date);
		}
	}
	
	private Day v;
}
